﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace OA.Core.Helper
{
    public static class JsonHelper
    {
        public static List<T> DeserializeList<T>(string content)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(content))
                {
                    return JsonConvert.DeserializeObject<List<T>>(content);
                }
                else
                {
                    return new List<T>();
                }
            }
            catch(Exception ex)
            {
                return new List<T>();
            }

        }

        public static string SerializeObject<T>(T t)
        {
            return JsonConvert.SerializeObject(t);
        }

        public static T CopyInfo<T>(T t)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(t));
            }
            catch(Exception ex)
            {
                return default(T);
            }
           
        }

        public static string ToBase62<T>(T t)
        {
            string value= JsonConvert.SerializeObject(t);

            return value.ToBase62();
        }

        public static string ToBase64<T>(T t)
        {
            string value = JsonConvert.SerializeObject(t);            
            byte[] b = System.Text.Encoding.Default.GetBytes(value);
            return Convert.ToBase64String(b);
        }

         

        public static T DeserializeObj<T>(string content)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(content))
                {
                    return JsonConvert.DeserializeObject<T>(content);
                }
                else
                {
                    return default(T);
                }
            }
            catch
            {
                return default(T);
            }

        }
    }
}
